package goods

import (
	"net/http"

	"github.com/EDDYCJY/go-gin-example/pkg/app"
	"github.com/EDDYCJY/go-gin-example/pkg/e"
	"github.com/EDDYCJY/go-gin-example/pkg/util"
	"github.com/EDDYCJY/go-gin-example/service/goods_service"
	"github.com/gin-gonic/gin"
)

type AddGoodsVerify struct {
	GoodsCommonID int `alias:"商品 ID" form:"goods_common_id"  json:"goods_common_id" `
	GoodsID       int `alias:"商品 ID" form:"goods_id"  json:"goods_id" `
}

func AddGoods(c *gin.Context) {
	var (
		appG = app.Gin{C: c}
		form AddGoodsVerify
	)
	_, errCode := app.BindAndValidMsg(c, &form)
	if errCode != nil {
		appG.ResponseMsg(errCode, nil)
		return
	}

	var data interface{}
	Service := goods_service.AddGoods{
		GoodsCommonID: form.GoodsCommonID,
		GoodsID:       form.GoodsID,
	}
	data, err := Service.AddGoods()
	if err != nil {
		appG.ResponseMsg(err, nil)
		return
	}
	//异步日志
	appG.Response(http.StatusOK, e.SUCCESS, data)

}

type GetGoodsVerify struct {
	InstitutionID int `alias:"机构ID" form:"institution_id"  json:"institution_id" valid:"Required;"`
}

func GetGoods(c *gin.Context) {
	var (
		appG = app.Gin{C: c}
		form GetGoodsVerify
	)
	_, errCode := app.BindAndValidMsg(c, &form)
	if errCode != nil {
		appG.ResponseMsg(errCode, nil)
		return
	}

	var data interface{}
	Service := goods_service.GetGoods{
		InstitutionID: form.InstitutionID,
		PageNum:       util.GetPage(c),
		PageSize:      util.GetLimit(c),
	}
	data, err := Service.GetGoods()
	if err != nil {
		appG.ResponseMsg(err, nil)
		return
	}
	//异步日志
	appG.Response(http.StatusOK, e.SUCCESS, data)

}
